package session08.homework;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DownPanel extends JPanel {

    private JPanel jPanel;

    private JButton exitBtn;
    private JButton confirmBtn;
    private JButton resetBtn;

    //存取四位数，至关重要
    private int[] num = new int[4];
    private int choice = 1;

    public DownPanel(NumFrame numFrame) {
        this.setLayout(new BorderLayout());
        this.jPanel = new JPanel();
        this.exitBtn = new JButton("退出");
        this.confirmBtn = new JButton("确定");
        this.resetBtn = new JButton("重置");

        this.jPanel.add(this.exitBtn);
        this.jPanel.add(this.confirmBtn);

        String finalNum = Num();

        this.confirmBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                String s = numFrame.getUpP().getNumTxt().getText();
                if (!s.matches("[0-9]{4}")) {
                    //弹窗出现在当前设计页面的中间
                    JOptionPane.showMessageDialog(numFrame, "请输入四位数字");
                    return;
                }
                for (int i = 0; i < s.length() - 1; i++) {
                    for (int j = i + 1; j < s.length(); j++) {
                        if (s.charAt(i) == s.charAt(j)) {
                            JOptionPane.showMessageDialog(numFrame, "请输入不重复的四位数字");
                            return;
                        }
                    }
                }

                int number = Integer.parseInt(s);

                int a = number % 10; //个位
                int b = number / 10 % 10;  //十位
                int c = number / 10 / 10 % 10;  //百位
                int d = number / 1000;     //千位

                if (a != b && a != c && a != d && b != c && b != d && c != d && s.length() == 4) {
                    int lengthA = 0, lengthB = 0;
                    int[] temp = {d, c, b, a};
                    if (d == num[0] && c == num[1] && b == num[2] && a == num[3]) {
                        System.out.println("4A0B");
                        //System.out.println("恭喜你猜对了，游戏结束！");
                        JOptionPane.showMessageDialog(null, "恭喜你猜对了");
                        numFrame.getMidP().getTxtLab().setText("欢迎来到猜数字游戏！");
                        DownPanel.this.resetBtn.setEnabled(true);
                        DownPanel.this.confirmBtn.setEnabled(false);
                        DownPanel.this.choice = 1;
                    }
                    //计算A
                    for (int k = 0; k < num.length; k++) {
                        if (temp[k] == num[k]) {
                            lengthA++;
                        }
                    }
                    //计算A+B
                    for (int m = 0; m < num.length; m++) {
                        for (int j = 0; j < temp.length; j++) {
                            if (num[m] == temp[j]) {
                                lengthB++;
                                //因为数是不重复的，相等之后，便不会相等，没有继续下去的必要
                                break;
                            }
                        }
                    }
                    String tempnum = lengthA + "A" + (lengthB - lengthA) + "B";
                    if (!tempnum.equals("4A0B")) {
                        //System.out.println(lengthA + "A" + (lengthB - lengthA) + "B");
                        numFrame.getMidP().getTxtLab().setText(tempnum);
                        JOptionPane.showMessageDialog(null, "你猜错了，还剩下" + (7 - DownPanel.this.choice) + "次机会");
                    }
                    DownPanel.this.choice++;
                    if (DownPanel.this.choice > 7) {
                        DownPanel.this.resetBtn.setEnabled(true);
                        DownPanel.this.confirmBtn.setEnabled(false);
                        JOptionPane.showMessageDialog(null, "7次机会已用完，正确数字为" + finalNum);
                    }
                    //numFrame.getUpP().getNumTxt().setText("");
                }
            }
        });

        //重置
        this.resetBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DownPanel.this.choice = 1;
                DownPanel.this.resetBtn.setEnabled(false);
                DownPanel.this.confirmBtn.setEnabled(true);
                numFrame.getMidP().getTxtLab().setText("欢迎来到猜数字游戏！");
                numFrame.getUpP().getNumTxt().setText("");
                String resetNum = Num();
            }
        });

        this.jPanel.add(this.resetBtn);
        this.resetBtn.setEnabled(false);
        this.add(this.jPanel, BorderLayout.SOUTH);
    }

    public String Num() {
        String strNum = "";
        //生成4个不重复的0-9的数
        for (int i = 0; i < num.length; i++) {
            num[i] = (int) (Math.random() * 9);
            //System.out.println(num[i]);
            for (int j = 0; j < i; j++) {
                if (num[i] == num[j]) {
                    i--;
                    break;
                }
            }
        }
        //打印4位随机数
        for (int element : num) {
            System.out.print(element + " ");
            strNum += (Integer.toString(element));
        }
        System.out.println("");
        return strNum;
    }
}
